package com.zerous.framework.ui;

import com.zerous.framework.*;
import com.zerous.framework.utill.*;
import com.zerous.framework.math.*;

public class Button extends UIBase
{
	int oc = 0xffaaaaaa, dc = 0xff555555;
	Pixmap i, d;
	Vector2 s;
	boolean touch;
	//int count = 0;
	int state;
	boolean clickFlag = false;
	Box bound;
	
	public Button(double x, double y, double w, double h)
	{
		super(new Vector2(x, y).toPoint());
		s = new Vector2(w, h);
		bound = new Box((int)x, (int)y, (int)w, (int)h);
	}
	
	public Button(Pixmap idle, Pixmap down, int x, int y)
	{
		super(new Point(x, y));
		i = idle;
		d = down;
		s = new Vector2(i.getWidth(), i.getHeight());
		bound = new Box(x, y, s.getX(), s.getY());
	}
	public Button(Point po, Vector2 s)
	{
		super(po);
		this.s = s;
	}

	public void setOnClick(OnClick onclick)
	{
		this.onclick = onclick;
	}

	public void setOnTouch(OnTouch ontouch)
	{
		this.ontouch = ontouch;
	}

	@Override
	public void update()
	{
		//FIXIT:
		//touch = (Utils.tXMT(p.getX()) && Utils.tXLT(p.getX() + s.getX())) && (Utils.tYLT(p.getY() + s.getY()) && Utils.tYMT(p.getY()));
		
		touch = bound.isInside(Zex.input.getTouch(0)) || bound.isInside(Zex.input.getTouch(1));
		if(touch &&(Zex.input.getTouchType(0) != 1 || Zex.input.getTouchType(1) != 1))
		{
			if(ontouch != null) ontouch.down();
			state = Zex.input.getTouchType(0);
			clickFlag = true;
		}
		else if((Zex.input.getTouchType(0) == 1 || Zex.input.getTouchType(1) == 1)&& clickFlag)
		{
			if(onclick != null)
			{
				onclick.click();
			}
			if(ontouch != null) 
				ontouch.up();
				clickFlag = false;
		}
		else 
		{
			//if(touch) if(count < 1 && onclick != null) onclick.click();
			
			clickFlag = false;
		}
	}

	@Override
	public void draw()
	{
		
		if(i == null || d == null) Zex.graphics.drawRect(p.getX(), p.getY(), s.getX(), s.getY(), touch ? dc : oc);
		else Zex.graphics.drawPixmap(touch ? d : i, p.getX(), p.getY(), 255);
	}
}
